/** **********************************************************************************
 * @file    4.B.c
 * @mainpage
 * @date    2013-09-29 22:33
 * @author  Alexey Kulikov <alexeyqu@gmail.com>, group 376, DCAM MIPT
 * @version 1.00
 *
 * Censor the input.
 *
 * @par     The task.
 *          The program cencors some text in the input
 *          according to some rules mentioned in problem's task
**************************************************************************************/

#include <stdlib.h>
#include <stdio.h>

#define DEBUG

#ifdef DEBUG
    #define DBG printf ("# "),
    #define ASSERT(cond)                                                              \
    if (!(cond))                                                                      \
    {                                                                                 \
        printf ("Critical failure in assertion '%s'! \nfile %s, line %d \n"           \
                "Now keep calm and EXTERMINATE! EXTERMINATE! EXTERMINATE!!!\n"        \
                , #cond, __FILE__, __LINE__);                                         \
        abort();                                                                      \
    }
#else
    #define DBG if (0)
    #define ASSERT(cond)
#endif

int Space_State = 0; //!< 1/0 if there was some free space in the input

/** ********************************************************************************
 * is_space - checks if the argument is space symbol
 *
 * @param   c   symbol
 *
 * @return  1 if it is, otherwise 0
************************************************************************************/
inline int is_space (unsigned char c);

/** ********************************************************************************
 * is_punctuation - checks if the argument is punctuation symbol
 *
 * @param   c   symbol
 *
 * @return  1 if it is, otherwise 0
************************************************************************************/
inline int is_punctuation (unsigned char c);

/** ********************************************************************************
 * is_digit - checks if the argument is digit
 *
 * @param   c   symbol
 *
 * @return  1 if it is, otherwise 0
************************************************************************************/
inline int is_digit (unsigned char c);

/** ********************************************************************************
 * is_capital - checks if the argument is capital letter
 *
 * @param   c   symbol
 *
 * @return  1 if it is, otherwise 0
************************************************************************************/
inline int is_capital (unsigned char c);

int main()
{
    DBG printf ("C language test'07. \n");
    DBG printf ("4.B problem. \n");
    DBG printf ("Input censoring filter. \n");
    DBG printf ("Enter text to censor: \n-> ");

    unsigned char c = 0; //!< Symbol in the input stream

    while ((c = getchar()) != EOF)
    {
        if (is_space (c))
        {
            if (Space_State) continue;
            else
            {
                Space_State = 1;
                printf (" ");
                continue;
            }
        }

        Space_State = 0;

        if (is_punctuation (c)) continue;

        if (is_digit (c)) {printf ("#"); continue;}

        if (is_capital (c)) {printf ("%c", c - 'A' + 'a'); continue;}

        printf ("%c", c);
    }

    return 0;
}

inline int is_space (unsigned char c)
{
    return (c == ' ' || c == '\t' || c == '\n')? 1 : 0;
}

inline int is_punctuation (unsigned char c)
{
    return (c == ',' ||
            c == '.' ||
            c == '?' ||
            c == '!' ||
            c == ':' ||
            c == ';' ||
            c == '\"' ||
            c == '\'')? 1 : 0;
}

inline int is_digit (unsigned char c)
{
    return (c >= '0' && c <= '9')? 1 : 0;
}

inline int is_capital (unsigned char c)
{
    return (c >= 'A' && c <= 'Z')? 1 : 0;
}
